package com.baseframe.mybatis.utils;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baseframe.tool.utils.data.BeanUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class BaseMetaObjectUtil {

    /**
     * 获取update时自动填充的字段名
     * @param clazz 类
     * @return 属性数组
     */
    public static String[] getUpdateFillPropertiesName(Class<?> clazz){

        Field[] allFields = BeanUtil.getWrapper().getAllFields(clazz);

        List<String> updateFillPropertiesNameList = new ArrayList<>();
        for (Field fieldName : allFields) {
            TableField tableField = fieldName.getAnnotation(TableField.class);
            if (tableField==null){
                continue;
            }
            FieldFill fieldFill = tableField.fill();
            if (fieldFill==FieldFill.UPDATE||fieldFill==FieldFill.INSERT_UPDATE){
                updateFillPropertiesNameList.add(fieldName.getName());
            }
        }

        return updateFillPropertiesNameList.toArray(new String[0]);
    }


    /**
     * 获取insert时，自动填充的字段名
     * @param clazz 类
     * @return 属性数组
     */
    public static String[] getInsertFillPropertiesName(Class<?> clazz){
        Field[] allFields = BeanUtil.getWrapper().getAllFields(clazz);

        List<String> updateFillPropertiesNameList = new ArrayList<>();
        for (Field fieldName : allFields) {
            TableField tableField = fieldName.getAnnotation(TableField.class);
            if (tableField==null){
                continue;
            }
            FieldFill fieldFill = tableField.fill();
            if (fieldFill==FieldFill.INSERT||fieldFill==FieldFill.INSERT_UPDATE){
                updateFillPropertiesNameList.add(fieldName.getName());
            }
        }

        return updateFillPropertiesNameList.toArray(new String[0]);
    }
}
